package com.swfee.qinben.base;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.swfee.qinben.R;

public abstract class TabViewflipperBaseActivity extends AppBaseActivity {

	private Animation slideLeftIn;
	private Animation slideLeftOut;
	private Animation slideRightIn;
	private Animation slideRightOut;

	private ViewFlipper viewFlipper;

	TextView tab1;
	TextView tab2;
	TextView tab3;

	private int viewIndex = 1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(getLayoutId());
		viewFlipper = (ViewFlipper) findViewById(R.id.app_flipper);
		slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.push_left_in);
		slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.push_left_out);
		slideRightIn = AnimationUtils.loadAnimation(this, R.anim.push_right_in);
		slideRightOut = AnimationUtils.loadAnimation(this,
				R.anim.push_right_out);
		initHeaderClick();

	}

	public void initHeaderClick() {
		tab1 = (TextView) findViewById(R.id.tab_1);
		tab2 = (TextView) findViewById(R.id.tab_2);
		tab3 = (TextView) findViewById(R.id.tab_3);
		tab1.setTextColor(getResources().getColor(R.color.white));
		final LinearLayout vl = (LinearLayout) findViewById(R.id.header_tab);
		tab1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				viewFlipper.setInAnimation(slideLeftIn);
				viewFlipper.setOutAnimation(slideRightOut);
				if (viewIndex == 2) {
					viewFlipper.showPrevious();
					vl.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.bg_header_1_select));
				} else if (viewIndex == 3) {
					viewFlipper.showPrevious();
					viewFlipper.showPrevious();
					vl.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.bg_header_1_select));
				}
				viewIndex = 1;
				tab1.setTextColor(getResources().getColor(R.color.white));
				tab2.setTextColor(getResources().getColor(R.color.black));
				tab3.setTextColor(getResources().getColor(R.color.black));
			}
		});
		tab2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (viewIndex == 1) {
					viewFlipper.setInAnimation(slideRightIn);
					viewFlipper.setOutAnimation(slideLeftOut);
					viewFlipper.showNext();
					vl.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.bg_header_2_select));
				} else if (viewIndex == 3) {
					viewFlipper.setInAnimation(slideLeftIn);
					viewFlipper.setOutAnimation(slideRightOut);
					viewFlipper.showPrevious();
					vl.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.bg_header_2_select));
				}
				viewIndex = 2;
				tab2.setTextColor(getResources().getColor(R.color.white));
				tab1.setTextColor(getResources().getColor(R.color.black));
				tab3.setTextColor(getResources().getColor(R.color.black));

			}
		});
		tab3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				viewFlipper.setInAnimation(slideRightIn);
				viewFlipper.setOutAnimation(slideLeftOut);
				if (viewIndex == 2) {
					viewFlipper.showNext();
					vl.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.bg_header_3_select));
				} else if (viewIndex == 1) {
					viewFlipper.showNext();
					viewFlipper.showNext();
					vl.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.bg_header_3_select));
				}
				viewIndex = 3;
				tab3.setTextColor(getResources().getColor(R.color.white));
				tab2.setTextColor(getResources().getColor(R.color.black));
				tab1.setTextColor(getResources().getColor(R.color.black));

			}
		});
	}

	public abstract int getLayoutId();

	public void setTabTitle(String t1, String t2, String t3) {
		tab1.setText(t1);
		tab2.setText(t2);
		tab3.setText(t3);
	}

}